home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / scheme / schmlbrr / schem_lb.lha / unsupported / general / blond.scm next >
Encoding:
Text File  |  1993-07-16  |  44.8 KB  |  1,388 lines

  1. ;;; From mcvax!diku.dk!danvy@uunet.UU.NET Wed Nov 16 20:38:55 1988
  2. ;;; Date:  Thu, 6 Oct 88 15:49:55 +0100
  3. ;;; From: mcvax!diku.dk!danvy@uunet.UU.NET (Olivier Danvy)
  4. ;;; To: scheme-librarian@zurich.ai.mit.edu
  5. ;;; Subject: submission
  6. ;;; 
  7. ;;; 
  8. ;;;     Dear librarian,
  9. ;;; 
  10. ;;;     here is the source code for the Blond reflective tower
  11. ;;;     as described in the article "Intensions and Extensions
  12. ;;;     in a Reflective Tower", at the LFP'88 conference.
  13. ;;;     Would you find it convenient to have the LaTex source
  14. ;;;     of the manual (25 pages), too?
  15. ;;; 
  16. ;;;     Keep in touch.
  17. ;;; 
  18. ;;;         Kind regards,        Olivier
  19. ;;; 
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ; blond-88.scm -- commented listing of Blond in -*- Scheme -*- version 0.8
  22. ;
  23. ;
  24. ; This is the non-reflective simulator of a reflective tower described in
  25. ;
  26. ;               Intensions and Extensions in a Reflective Tower
  27. ;
  28. ; pp 327-341 of the proceedings of the
  29. ;           1988 ACM Conference on Lisp and Functional Programming
  30. ;
  31. ;
  32. ;                                       Olivier Danvy & Karoline Malmkjaer
  33. ;                                       DIKU -- Computer Science Department
  34. ;                                       University of Copenhagen
  35. ;                                       Universitetsparken 1
  36. ;                                       DK-2100 Copenhagen O
  37. ;                                       Denmark
  38. ;
  39. ;                                       e-mail: danvy@diku.dk
  40. ;                                               karoline@diku.dk
  41. ;                                       from US: mcvax!diku!danvy@uunet.uu.net
  42. ;
  43. ;                                       Phone: (45) 1 39 64 66 and ask
  44. ; -----------------------------------------------------------------------------
  45.  
  46.  
  47. ;                                               Copenhagen, January-August 1988
  48.  
  49. ; Use: under Scheme, load "blond.scm"
  50. ; then type (blond)
  51.  
  52. ; This interpreter is totally meta-circular.
  53. ; It can be loaded in a blond session with
  54. ;       (load "blond.scm")
  55. ; and started with
  56. ;       (blond)
  57. ; at the price of a certain slowness,
  58. ; but starting a reflective tower orthogonal to the current one.
  59. ;
  60. ; Note: as it stands here, Blond is not perfect, and has already been
  61. ; considerably improved. But it has the advantage to be faithful
  62. ; to the LFP'88 paper and to have an accurate manual.
  63. ; -----------------------------------------------------------------------------
  64.  
  65.  
  66. ; Domains:
  67.  
  68. ; Denotable values = expressible values = storable values:
  69. ; Val = Num + String + Ide + Pair +
  70. ;       Abstraction + Subr + Fsubr +
  71. ;       Environment + Continuation +
  72. ;       DeltaReifier + GammaReifier
  73.  
  74. ; Answers:
  75. ; Ans = Val + {_|_}
  76.  
  77. ; Meta-continuations:
  78. ; Meta-Cont = (Env x Cont) x Meta-Cont
  79.  
  80. ; Environments and continuations:
  81. ; Env = (Ide* x Val*)*          -- lexical extensions, then global, then common
  82. ; Cont = Val x MC -> Ans
  83.  
  84. ; Procedures, primitive functions and control structures:
  85. ; Lambda-Abstraction =  Val* x Cont x MC -> Ans
  86. ; Subr = Val* -> Val
  87. ; Fsubr = Expr* x Env x Cont  x MC
  88.  
  89. ; Reified environments and continuations:
  90. ; Environment = (Unit -> Val) + (Ide -> Val) + (Ide x Val -> Val)
  91. ; Continuation = Cont
  92.  
  93. ; Reifiers:
  94. ; Delta-Reifier = Val x Val x Val x Env x Cont x MC -> Ans
  95. ; Gamma-Reifier = Val x Val x Val x Cont x MC -> Ans
  96.  
  97.  
  98. ; ----- the core --------------------------------------------------------------
  99. ; A Blond expression is either a constant (that are left as they are),
  100. ; an identifier (that is looked up) or a pair (that represents a redexe).
  101.  
  102. ; Expr * Env * Cont * Meta-Cont -> Val
  103. (define _eval
  104.     (lambda (e r k tau)
  105.         (cond
  106.             ((_constant? e)
  107.                 (k e tau))
  108.             ((_identifier? e)
  109.                 (_lookup e r k tau))
  110.             ((pair? e)
  111.                 (_eval (car e)
  112.                        r
  113.                        (lambda (f tau)
  114.                            (_apply f (cdr e) r k tau))
  115.                        tau))
  116.             (else
  117.                  (_wrong '_eval "unknown form" e)))))
  118.  
  119.  
  120. ; An identifier is first looked up in the current lexical extension of
  121. ; the environment, then in the global environment of the current level,
  122. ; and lastly in the common environment.
  123.  
  124. ; Ide * Env * Cont * Meta-Cont -> Val
  125. (define _lookup
  126.     (lambda (i r k tau)
  127.         (let ((pos (_index i (caar r))))
  128.             (if (isNatural? pos)
  129.                 (k (_access (_nth pos (cdar r))) tau)
  130.                 (if (null? (cdr r))
  131.                     (_lookup_common i k tau)
  132.                     (_lookup i (cdr r) k tau))))))
  133.  
  134. ; Ide * Cont * Meta-Cont -> Val
  135. (define _lookup_common
  136.     (lambda (i k tau)
  137.         (let ((pos (_index i table-common-identifiers)))
  138.             (if (isNatural? pos)
  139.                 (k (_access (_nth pos table-common-values)) tau)
  140.                 (_wrong '_lookup_common "unbound identifier" i)))))
  141.  
  142.  
  143. ; Applying an applicable object dispatches on its injection tag.
  144.  
  145. ; Fun * List-of-Expr * Env * Cont * Meta-Cont -> Val
  146. (define _apply
  147.     (lambda (fo l r k tau)
  148.         (if (_applicable? fo)
  149.             (case (_fetch-ftype fo)
  150.                 ((subr)
  151.                     (_apply_subr fo l r k tau))
  152.                 ((fsubr)
  153.                     (_apply_fsubr fo l r k tau))
  154.                 ((lambda-abstraction)
  155.                     (_apply_procedure fo l r k tau))
  156.                 ((delta-abstraction)
  157.                     (_apply_delta-reifier fo l r k tau))
  158.                 ((gamma-abstraction)
  159.                     (_apply_gamma-reifier fo l r k tau))
  160.                 ((environment)
  161.                     (_apply_environment fo l r k tau))
  162.                 ((continuation)
  163.                     (_apply_continuation fo l r k tau))
  164.                 (else
  165.                      (_wrong '_apply "unknown functional object" (car fo))))
  166.             (_wrong '_apply "unapplicable form" fo))))
  167.  
  168.  
  169. ; Applying a primitive function dispatches on its arity. There are
  170. ; currently nullary, unary, binary, and ternary primitive functions.
  171.  
  172. ; Subr * List-of-Expr * Env * Cont * Meta-Cont -> Val
  173. (define _apply_subr
  174.     (lambda (f l r k tau)
  175.         (if (not (= (length l) (_fetch-arity f)))
  176.             (_wrong '_apply_subr "arity mismatch" l)
  177.             (case (_fetch-arity f)
  178.                 (0
  179.                     (k ((_fetch-value f)) tau))
  180.                 (1
  181.                     (_eval (car l) r (lambda (a tau)
  182.                                          (k ((_fetch-value f) a) tau)) tau))
  183.                 (2
  184.                     (_eval (car l)
  185.                            r
  186.                            (lambda (a1 tau)
  187.                                (_eval (cadr l)
  188.                                       r
  189.                                       (lambda (a2 tau)
  190.                                           (k ((_fetch-value f) a1 a2) tau))
  191.                                       tau))
  192.                            tau))
  193.                 (3
  194.                     (_eval (car l)
  195.                            r
  196.                            (lambda (a1 tau)
  197.                                (_eval (cadr l)
  198.                                       r
  199.                                       (lambda (a2 tau)
  200.                                           (_eval (caddr l)
  201.                                                  r
  202.                                                  (lambda (a3 tau)
  203.                                                      (k ((_fetch-value f)
  204.                                                             a1 a2 a3) tau))
  205.                                                  tau))
  206.                                       tau))
  207.                            tau))
  208.                 (else
  209.                     (_wrong '_apply_subr "arity" f))))))
  210.  
  211.  
  212. ; Before reducing a special form, its arity is checked.
  213.  
  214. ; Fsubr * List-of-Expr * Env * Cont * Meta-Cont -> Val
  215. (define _apply_fsubr
  216.     (lambda (fv l r k tau)
  217.         (if (or (= (length l) (_fetch-arity fv))
  218.                 (zero? (_fetch-arity fv)))      ; arbitrary number of arguments
  219.             ((_fetch-value fv) l r k tau)
  220.             (_wrong '_apply_fsubr "arity mismatch" l))))
  221.  
  222.  
  223. ; The arity of procedures is also checked:
  224.  
  225. ; Lambda-Abstraction * List-of-Expr * Env * Cont * Meta-Cont -> Val
  226. (define _apply_procedure
  227.     (lambda (p l r k tau)
  228.         (if (not (= (length l) (_fetch-arity p)))
  229.             (_wrong '_apply_procedure "arity mismatch" l)
  230.             (_evlis l r (lambda (lv tau)
  231.                             ((_fetch-value p) lv k tau)) tau))))
  232.  
  233.  
  234. ; A sequence of expressions is evaluated from left to right:
  235.  
  236. ; List-of-Expr * Env * Cont * Meta-Cont -> Val
  237. (define _evlis
  238.     (lambda (l r k tau)
  239.         (if (null? l)
  240.             (k () tau)
  241.             (_eval (car l)
  242.                    r
  243.                    (lambda (v tau)
  244.                        (_evlis (cdr l)
  245.                                r
  246.                                (lambda (lv tau)
  247.                                    (k (cons v lv) tau))
  248.                                tau))
  249.                    tau))))
  250.  
  251.  
  252. ; Applying a reified environment gives access to its representation,
  253. ; looks up an identifier, or assigns it, according to the number of arguments.
  254.  
  255. ; Reified-Env * List-of-Expr * Env * Cont * Meta-Cont -> Val
  256. (define _apply_environment
  257.     (lambda (f l r k tau)
  258.         (case (length l)
  259.             (0
  260.                 (k (_env-down f) tau))
  261.             (1
  262.                 (_eval (car l)
  263.                        r
  264.                        (lambda (i tau)
  265.                            (if (_identifier? i)
  266.                                (k (_R_lookup i (_env-down f)) tau)
  267.                                (_wrong '_apply_environment
  268.                                        "not an identifier"
  269.                                        i)))
  270.                        tau))
  271.             (2
  272.                 (_eval (car l)
  273.                        r
  274.                        (lambda (i tau)
  275.                            (_eval (cadr l)
  276.                                   r
  277.                                   (lambda (v tau)
  278.                                       (_apply_environment_set! i v f k tau))
  279.                                   tau))
  280.                        tau))
  281.             (else
  282.                  (_wrong '_apply_environment "arity mismatch" l)))))
  283.  
  284.  
  285. ; Ide * Reified-Env -> Val
  286. (define _R_lookup
  287.     (lambda (i r)
  288.         (let ((pos (_index i (caar r))))
  289.             (if (isNatural? pos)
  290.                 (_access (_nth pos (cdar r)))
  291.                 (if (null? (cdr r))
  292.                     (_R_lookup_common i)
  293.                     (_R_lookup i (cdr r)))))))
  294.  
  295. ; Ide -> Val
  296. (define _R_lookup_common
  297.     (lambda (i)
  298.         (let ((pos (_index i table-common-identifiers)))
  299.             (if (isNatural? pos)
  300.                 (_access (_nth pos table-common-values))
  301.                 '***undefined***))))
  302.  
  303.  
  304. ; Ide * Val * Reified-Env * Cont * Meta-Cont -> Val
  305. (define _apply_environment_set!
  306.     (lambda (i v f k tau)
  307.         (if (_identifier? i)
  308.             (let ((location (_L_lookup i (_env-down f))))
  309.                 (if (null? location)
  310.                     (_wrong '_apply_environment "undefined variable" i)
  311.                     (let ((o (_access location)))
  312.                         (begin
  313.                             (_update location v)
  314.                             (k o tau)))))
  315.             (_wrong '_apply_environment "not an identifier" i))))
  316.  
  317. ; Ide * Reified-Env -> Loc
  318. (define _L_lookup
  319.     (lambda (i r)
  320.         (let ((pos (_index i (caar r))))
  321.             (if (isNatural? pos)
  322.                 (_nth pos (cdar r))
  323.                 (if (null? (cdr r))
  324.                     (_L_lookup_common i r)
  325.                     (_L_lookup i (cdr r)))))))
  326.  
  327. ; Ide * Reified-Environment -> Loc
  328. (define _L_lookup_common
  329.     (lambda (i r)
  330.         (let ((pos (_index i table-common-identifiers)))
  331.             (if (isNatural? pos)
  332.                 (begin
  333.                     (set-car! (car r)
  334.                               (cons i (caar r)))
  335.                     (set-cdr! (car r)
  336.                               (cons (_access (_nth pos table-common-values))
  337.                                     (cdar r)))
  338.                     (cdar r))
  339.                 '()))))
  340.  
  341.  
  342. ; Applying a continuation can be done jumpily or pushily. In the first case,
  343. ; the current continuation is ignored; in the second, the current
  344. ; environment and continuation are pushed onto the meta-continuation.
  345.  
  346. ; Reified-Cont * List-of-Expr * Env * Cont * Meta-Cont -> Val
  347. (define _apply_continuation-jumpy
  348.     (lambda (c l r k tau)
  349.         (if (= (length l) 1)
  350.             (_eval (car l) r (_cont-down c) tau)
  351.             (_wrong '_apply_continuation-jumpy "arity mismatch" l))))
  352.  
  353. ; Reified-Cont * List-of-Expr * Env * Cont * Meta-Cont -> Val
  354. (define _apply_continuation-pushy
  355.     (lambda (c l r k tau)
  356.         (if (= (length l) 1)
  357.             (_eval (car l) r (_cont-down c) (_meta-push r k tau))
  358.             (_wrong '_apply_continuation-pushy "arity mismatch" l))))
  359.  
  360. ; Hook for the toggle switch-continuation-mode:
  361. (define _apply_continuation _apply_continuation-jumpy)
  362.  
  363.  
  364.  
  365. ; Applying a reifier reifies its arguments, the current environment and
  366. ; the current continuation:
  367.  
  368. ; Delta-Reifier * List-of-Expr * Env * Cont * Meta-Cont -> Val
  369. (define _apply_delta-reifier
  370.     (lambda (d l r k tau)
  371.         ((_untag d) (_exp-up* l) (_env-up r) (_cont-up k)
  372.                     (_top-env tau) (_top-cont tau) (_meta-pop tau))))
  373.  
  374.  
  375. ; Gamma-Reifier * List-of-Expr * Env * Cont * Meta-Cont -> Val
  376. (define _apply_gamma-reifier
  377.     (lambda (g l r k tau)
  378.         ((_untag g) (_exp-up* l) (_env-up r) (_cont-up k)
  379.                     (_top-cont tau) (_meta-pop tau))))
  380.  
  381.  
  382. ; List-of-Expr -> List-of-Exp
  383. (define _exp-up*
  384.     (lambda (l)         ; (map copy l)
  385.         l))
  386.  
  387. (define _exp-up
  388.     (lambda (e)
  389.         e))             ; (copy e)
  390.  
  391. ; Env -> Reified-Env
  392. (define _env-up
  393.     (lambda (r)
  394.         (cons 'environment (lambda () r))))
  395.  
  396. ; Cont -> Reified-Cont
  397. (define _cont-up
  398.     (lambda (k)
  399.         (cons 'continuation k)))
  400.  
  401. (define _untag cdr)
  402.  
  403.  
  404.  
  405. ; Reflecting spawns a new level.
  406.  
  407. ; List-of-Expr * Env * Cont * Meta-Cont -> Val
  408. (define _meaning
  409.     (lambda (l r k tau)
  410.         (_eval (car l)
  411.                r
  412.                (lambda (a1 tau)
  413.                    (_eval (cadr l)
  414.                           r
  415.                           (lambda (a2 tau)
  416.                               (_eval (caddr l)
  417.                                      r
  418.                                      (lambda (a3 tau)
  419.                                          (_check_and_spawn a1 a2 a3 r k tau))
  420.                                      tau))
  421.                           tau))
  422.                tau)))
  423.  
  424. ; Val * Val * Val * Env * Cont * Meta-Cont -> Val
  425. (define _check_and_spawn
  426.     (lambda (a1 a2 a3 r k tau)
  427.         (cond
  428.             ((not (_expressible? a1))
  429.                 (_wrong '_meaning "non-expressible value" a1))
  430.             ((not (_ecological? a2))
  431.                 (_wrong '_meaning "polluted environment" a2))
  432.             ((not (_continuable? a3))
  433.                 (_wrong '_meaning "pitfall" a3))
  434.             (else
  435.                 (_spawn (_exp-down a1) (_env-down a2)
  436.                         a3      ; _spawn is going to _cont-down a3
  437.                         r k tau)))))
  438.  
  439. ; Expr -> Bool
  440. (define _expressible?           ; safe
  441.     (lambda (x)
  442.         (or (constant? x)
  443.             (_identifier? x)
  444.             (and (pair? x)
  445.                 (_expressible? (car x))
  446.                 (or (null? (cdr x))
  447.                     (and (pair? (cdr x))
  448.                         (_expressible? (cdr x))))))))
  449.  
  450. ; Expr -> Bool
  451. (define _expressible?           ; cheaper
  452.     (lambda (x)
  453.         'true))
  454.  
  455. ; Reified-Env -> Bool
  456. (define _environment?           ; naive: one could build such an "environment"
  457.     (lambda (x)                 ; changing the tag of a reified continuation!
  458.         (and (pair? x)          ; he would certainly have what he deserves
  459.             (equal? (car x) 'environment)
  460.             (procedure? (cdr x)))))
  461.  
  462. (define _ecological? _environment?)
  463.  
  464. ; Expr -> Bool
  465. (define _continuable?
  466.     (lambda (x)
  467.         (and (_applicable? x)
  468.             (case (_fetch-ftype x)
  469.                 ((subr)
  470.                     (= (_fetch-arity x) 1))
  471.                 ((fsubr)
  472.                     (= (_fetch-arity x) 1))
  473.                 ((lambda-abstraction)
  474.                     (= (_fetch-arity x) 1))
  475.                 ((delta-abstraction gamma-abstraction environment continuation)
  476.                     #t)
  477.                 (else
  478.                     #f)))))
  479.  
  480. ; Expr -> Expr
  481. (define _exp-down
  482.     (lambda (x)
  483.         x))
  484.  
  485. ; Reified-Env -> Env
  486. (define _env-down
  487.     (lambda (r)
  488.         (_unwrap-env (cdr r))))
  489.  
  490. ; Reified-Env-without-injection-tag -> Env
  491. (define _unwrap-env
  492.     (lambda (r)
  493.         (r)))
  494.  
  495. ; Expr -> Cont
  496. (define _cont-down cdr)
  497.  
  498.  
  499.  
  500.  
  501. ; Expr * Env * Cont * Env * Cont * Meta-Cont -> Val
  502. (define _spawn
  503.     (lambda (_e _r _k r k tau)
  504.         (case (_fetch-ftype _k)
  505.             (subr
  506.                 (_eval _e
  507.                       _r
  508.                       (lambda (a tau)
  509.                           (_terminate-level ((_fetch-value _k) a) tau))
  510.                       (_meta-push r k tau)))
  511.             (fsubr              ; adventurous
  512.                 ((_fetch-value _k)
  513.                      (list _e) _r _terminate-level (_meta-push r k tau)))
  514.             (lambda-abstraction
  515.                 (_eval _e
  516.                        _r
  517.                        (lambda (a tau)
  518.                            ((_fetch-value _k) (list a)
  519.                                              (_top-cont tau)
  520.                                              (_meta-pop tau)))
  521.                        (_meta-push r k tau)))
  522.             (delta-abstraction
  523.                 ((_untag d) (_exp-up _e) (_env-up _r)
  524.                             (_cont-up _terminate-level)
  525.                             r k tau))
  526.             (gamma-abstraction
  527.                 ((_untag g) (_exp-up _e) (_env-up _r)
  528.                             (_cont-up _terminate-level)
  529.                             k tau))
  530.             (environment
  531.                 (_eval _e
  532.                        _r
  533.                        (lambda (i tau)
  534.                            (if (_identifier? i)
  535.                                (_terminate-level (_R_lookup i
  536.                                                             (_env-down _k))
  537.                                                  tau)
  538.                                (_wrong '_environment
  539.                                        "not an identifier"
  540.                                        i)))
  541.                        (_meta-push r k tau)))
  542.             (continuation
  543.                 (_eval _e _r (_cont-down _k) (_meta-push r k tau))))))
  544.  
  545.  
  546.  
  547. ; Terminating a level transmits the result to the level above:
  548.  
  549. ; Val * Meta-Cont -> Val
  550. (define _terminate-level
  551.     (lambda (a tau)
  552.         ((_top-cont tau) a (_meta-pop tau))))
  553.  
  554.  
  555. ; An applicable object is built out of injection tags and an actual value:
  556.  
  557. (define _applicable?
  558.     (lambda (x)
  559.         (and (pair? x)
  560.              (case (car x)
  561.                  ((subr fsubr lambda-abstraction)
  562.                     (and (= 3 (length x))
  563.                          (number? (cadr x))
  564.                          (procedure? (caddr x))))
  565.                  ((delta-abstraction gamma-abstraction)
  566.                     (procedure? (cdr x)))
  567.                  ((environment continuation)
  568.                     (procedure? (cdr x)))
  569.                  (else
  570.                     #f)))))
  571.  
  572.  
  573. ; ----- the values in the initial environment ---------------------------------
  574.  
  575. ; Evaluating a value designated by quote dereferences it:
  576. (define _quote
  577.     (lambda (l r k tau)
  578.         (k (car l) tau)))
  579.  
  580.  
  581. ; As in Scheme, booleans are #t and #f, and in addition,
  582. ; the empty list stands for false, and anything that is not false is true:
  583. (define _if
  584.     (lambda (l r k tau)
  585.         (_eval (car l) r (lambda (a tau)
  586.                              (case a
  587.                                  ((#t)
  588.                                      (_eval (cadr l) r k tau))
  589.                                  ((#f)
  590.                                      (_eval (caddr l) r k tau))
  591.                                  (else
  592.                                      (if (null? a)
  593.                                          (_eval (caddr l) r k tau)
  594.                                          (_eval (cadr l) r k tau))))) tau)))
  595.  
  596.  
  597. ; lambda, delta, and gamma-abstractions evaluate to functions and reifiers:
  598. (define _lambda
  599.     (lambda (l r k tau)
  600.         (k (_inLambda-Abstraction (length (car l))
  601.                                   (lambda (lv k tau)
  602.                                       (_eval (cadr l)
  603.                                              (_extend_env (car l) lv r)
  604.                                              k
  605.                                              tau)))
  606.            tau)))
  607.  
  608. (define _inLambda-Abstraction
  609.     (lambda (n a)
  610.         (list 'lambda-abstraction n a)))
  611.  
  612.  
  613. (define _delta
  614.     (lambda (l r k tau)
  615.         (if (not (= (length (car l)) 3))
  616.             (_wrong '_delta "list of formal parameters" (car l))
  617.             (k (_inDelta-Abstraction (lambda (ee rr kk rho kappa tau)
  618.                                          (_eval (cadr l)
  619.                                                 (_extend_env (car l)
  620.                                                          (list ee rr kk)
  621.                                                          rho)
  622.                                                 kappa
  623.                                                 tau)))
  624.                tau))))
  625.  
  626. (define _inDelta-Abstraction
  627.     (lambda (a)
  628.         (cons 'delta-abstraction a)))
  629.  
  630.  
  631. (define _gamma
  632.     (lambda (l r k stau)
  633.         (if (not (= (length (car l)) 3))
  634.             (_wrong '_gamma "list of formal parameters" (car l))
  635.             (k (_inGamma-Abstraction (lambda (ee rr kk kappa tau)
  636.                                           (_eval (cadr l)
  637.                                                  (_extend_env (car l)
  638.                                                           (list ee rr kk)
  639.                                                           (_top-env stau))
  640.                                                  kappa
  641.                                                  tau)))
  642.                stau))))
  643.  
  644. (define _inGamma-Abstraction
  645.     (lambda (a)
  646.         (cons 'gamma-abstraction a)))
  647.  
  648.  
  649.  
  650. ; A common definition affects the common environment:
  651. (define _common-define
  652.     (lambda (l r k tau)
  653.         (if (not (_identifier? (car l)))
  654.             (_wrong '_common-define "undefinable" (car l))
  655.             (_eval
  656.                 (cadr l)
  657.                 r
  658.                 (lambda (a tau)
  659.                     (let ((pos (_index (car l) table-common-identifiers)))
  660.                         (if (isNatural? pos)
  661.                             (begin
  662.                                 (_update (_nth pos table-common-values) a)
  663.                                 (k (car l) tau))
  664.                             (begin
  665.                                 (set! table-common-identifiers
  666.                                       (cons (car l) table-common-identifiers))
  667.                                 (set! table-common-values
  668.                                       (cons a table-common-values))
  669.                                 (k (car l) tau))))) tau))))
  670.  
  671.  
  672.  
  673. ; A definition affects the global environment of the current level.
  674. (define _define
  675.     (lambda (l r k tau)
  676.         (if (not (_identifier? (car l)))
  677.             (_wrong '_define "undefinable" (car l))
  678.             (_eval
  679.                 (cadr l)
  680.                 r
  681.                 (lambda (a tau)
  682.                     (let* ((global-env (car (last-pair r)))
  683.                            (pos (_index (car l) (car global-env))))
  684.                         (if (isNatural? pos)
  685.                             (begin
  686.                                 (_update (_nth pos (cdr global-env)) a)
  687.                                 (k (car l) tau))
  688.                             (begin
  689.                                 (set-car! global-env
  690.                                           (cons (car l) (car global-env)))
  691.                                 (set-cdr! global-env
  692.                                       (cons a (cdr global-env)))
  693.                                 (k (car l) tau))))) tau))))
  694.  
  695.  
  696.  
  697. ; An assignment affects the representation of the environment. Assigning
  698. ; a common identifier shadows it at the current level.
  699. (define _set!
  700.     (lambda (l r k tau)
  701.         (if (not (_identifier? (car l)))
  702.             (_wrong '_set! "undefinable" (car l))
  703.             (_eval (cadr l) r (lambda (a tau)
  704.                                   (_L_set! (car l) a r k tau)) tau))))
  705.  
  706. (define _L_set!
  707.     (lambda (i v r k tau)
  708.         (let ((pos (_index i (caar r))))
  709.             (if (isNatural? pos)
  710.                 (let* ((location (_nth pos (cdar r)))
  711.                        (previous-value (_access location)))
  712.                     (begin
  713.                         (_update location v)
  714.                         (k previous-value tau)))
  715.                 (if (null? (cdr r))
  716.                     (let ((pos (_index i table-common-identifiers)))
  717.                         (if (isNatural? pos)
  718.                             (begin
  719.                                 (set-car! (car r) (cons i (caar r)))
  720.                                 (set-cdr! (car r) (cons v (cdar r)))
  721.                                 (k (_access (_nth pos table-common-values))
  722.                                    tau))
  723.                             (_wrong '_L_set! "undefined variable" i)))
  724.                     (_L_set! i v (cdr r) k tau))))))
  725.  
  726.  
  727.  
  728. ; The extensional if, that evaluates all its arguments:
  729. (define _ef
  730.     (lambda (p at af)
  731.         (case p
  732.             ((#t)
  733.                 at)
  734.             ((#f)
  735.                 af)
  736.             (else
  737.                 (if (null? p) af at)))))
  738.  
  739.  
  740. ; The case statement:
  741. (define _case 
  742.     (lambda (l r k tau)
  743.         (_eval (car l) r (lambda (a tau)
  744.                                 (_case_loop a (cdr l) r k tau)) tau)))
  745.  
  746. (define _case_loop
  747.     (lambda (a l r k tau)
  748.         (if (null? l)
  749.             (_wrong '_case_loop "unmatched" a)
  750.             (if (equal? (caar l) 'else)
  751.                 (_eval (cadr (car l)) r k tau)
  752.                 (if ((if (pair? (caar l)) member equal?) a (caar l))
  753.                     (_eval (cadr (car l)) r k tau)
  754.                     (_case_loop a (cdr l) r k tau))))))
  755.  
  756.  
  757. ; The conjunctive expression:
  758. (define _and
  759.     (lambda (l r k tau)
  760.         (if (null? l)
  761.             (k #t tau)
  762.             (_and_loop l r k tau))))
  763.  
  764. (define _and_loop
  765.     (lambda (l r k tau)
  766.         (if (null? (cdr l))
  767.             (_eval (car l) r k tau)
  768.             (_eval (car l) r (lambda (a tau)
  769.                                  (if (or (null? a) (equal? a #f))
  770.                                      (k #f tau)
  771.                                      (_and_loop (cdr l) r k tau))) tau))))
  772.  
  773.  
  774. ; The disjunctive expression:
  775. (define _or
  776.     (lambda (l r k tau)
  777.         (if (null? l)
  778.             (k #f tau)
  779.             (_or_loop l r k tau))))
  780.  
  781. (define _or_loop
  782.     (lambda (l r k tau)
  783.         (if (null? (cdr l))
  784.             (_eval (car l) r k tau)
  785.             (_eval (car l) r (lambda (a tau)
  786.                                  (if (or (null? a) (equal? a #f))
  787.                                      (_or_loop (cdr l) r k tau)
  788.                                      (k a tau))) tau))))
  789.  
  790.  
  791. ; The sequence statement:
  792. (define _begin
  793.     (lambda (l r k tau)
  794.         (if (null? (cdr l))
  795.             (_eval (car l) r k tau)
  796.             (_eval (car l) r (lambda (a tau)
  797.                                 (_begin (cdr l) r k tau)) tau))))
  798.  
  799.  
  800. ; Reading is done either from the implicit input stream
  801. ; or from an explicit port:
  802. (define _read
  803.     (lambda (l r k tau)
  804.         (if (null? l)
  805.             (k (read) tau)
  806.             (if (null? (cdr l))
  807.                 (_eval (car l)
  808.                        r
  809.                        (lambda (port tau)
  810.                            (k (read port) tau))
  811.                        tau)
  812.                 (_wrong '_read "arity mismatch" l)))))
  813.  
  814.  
  815. ; Loading a file redirects the input stream:
  816. (define _load
  817.     (lambda (l r k tau)
  818.         (_eval (car l)
  819.                r
  820.                (lambda (file tau)
  821.                    (_load_loop file (open-input-file file) r k tau))
  822.                tau)))
  823.  
  824. (define _load_loop
  825.     (lambda (file port r k tau)
  826.         (let ((it (read port)))
  827.             (if (eof-object? it)
  828.                 (begin
  829.                     (newline)
  830.                     (close-input-port port)
  831.                     (k file tau))
  832.                 (let ((a (_eval it r (lambda (a tau) (list 'okay a tau)) tau)))
  833.                     (if (equal? (car a) 'okay)
  834.                         (begin
  835.                             (display (cadr a)) (display " ") (flush-output)
  836.                             (_load_loop file port r k tau))
  837.                         (begin
  838.                             (close-input-port port)
  839.                             a)))))))
  840.  
  841.  
  842. ; A file can be loaded without displaying the results of the evaluations:
  843. (define _mute-load
  844.     (lambda (l r k tau)
  845.         (_eval (car l)
  846.                r
  847.                (lambda (file tau)
  848.                    (_mute-load_loop file (open-input-file file) r k tau))
  849.                tau)))
  850.  
  851. (define _mute-load_loop
  852.     (lambda (file port r k tau)
  853.         (let ((it (read port)))
  854.             (if (eof-object? it)
  855.                 (begin
  856.                     (close-input-port port)
  857.                     (k file tau))
  858.                 (let ((a (_eval it r (lambda (a tau) (list 'okay a tau)) tau)))
  859.                     (if (equal? (car a) 'okay)
  860.                         (_mute-load_loop file port r k tau)
  861.                         (begin
  862.                             (close-input-port port)
  863.                             a)))))))
  864.  
  865.  
  866.  
  867. ; A new interactive level can be spawned:
  868. (define _openloop
  869.     (lambda (l r k tau)
  870.         (case (length l)
  871.             (1
  872.                 (_eval (car l)
  873.                        r
  874.                        (lambda (new-level tau)
  875.                            ((_generate_toplevel-continuation
  876.                                         new-level (make-initial-environment))
  877.                                 blond-banner
  878.                                 (_meta-push r k tau)))
  879.                        tau))
  880.             (2
  881.                 (_eval (car l)
  882.                        r
  883.                        (lambda (new-level tau)
  884.                            (_eval (cadr l)
  885.                                   r
  886.                                   (lambda (new-env tau)
  887.                                       (if (_environment? new-env)
  888.                                           ((_generate_toplevel-continuation
  889.                                                         new-level
  890.                                                         (_env-down new-env))
  891.                                                 blond-banner
  892.                                                 (_meta-push r k tau))
  893.                                           (_wrong '_openloop
  894.                                                   "not a reified environment"
  895.                                                   new-env)))
  896.                                   tau))
  897.                        tau))
  898.             (else
  899.                 (_wrong '_openloop "wrong arity" l)))))
  900.  
  901.  
  902. ; Extending a reified environment needs reflecting it & reifying its extension:
  903. (define _access
  904.     car)
  905.  
  906. (define _update
  907.     set-car!)
  908.  
  909.  
  910. (define _extend-reified-environment
  911.     (lambda (l r k tau)
  912.         (_eval (car l)
  913.                r
  914.                (lambda (a1 tau)
  915.                    (_eval (cadr l)
  916.                           r
  917.                           (lambda (a2 tau)
  918.                               (_eval (caddr l)
  919.                                      r
  920.                                      (lambda (a3 tau)
  921.                                          (_extend a1 a2 a3 k tau))
  922.                                      tau))
  923.                           tau))
  924.                tau)))
  925.  
  926.  
  927. (define _extend
  928.     (lambda (li lv r k tau)
  929.         (cond
  930.             ((not (pair? li))
  931.                 (_wrong '_extend-reified-environment
  932.                         "not a list of identifiers"
  933.                         li))
  934.             ((not (pair? lv))        
  935.                 (_wrong '_extend-reified-environment
  936.                         "not a list of values"
  937.                         li))
  938.             ((not (= (length li) (length lv)))
  939.                 (_wrong '_extend-reified-environment
  940.                         "lists mismatch"
  941.                         (list li lv)))
  942.             ((not (_environment? r))
  943.                 (_wrong '_extend-reified-environment
  944.                         "not a reified environment"
  945.                         r))
  946.             (else
  947.                 (k (_env-up (_extend_env li lv (_env-down r))) tau)))))
  948.  
  949.  
  950.  
  951. ; The following describes the usual block structures let and letrec.
  952. ; A recursive binding is achieved by side-effect rather than by a fixed point.
  953. (define _let                    ; assumes a well-formed let construction
  954.     (lambda (l r k tau)
  955.         (if (null? (car l))
  956.             (_eval (cadr l) r k tau)
  957.             (_let_evlis (car l)
  958.                         r
  959.                         (lambda (lv tau)
  960.                             (_eval (cadr l)
  961.                                    (_extend_env (_let_idlis (car l)) lv r)
  962.                                    k
  963.                                    tau))
  964.                         tau))))
  965.  
  966. (define _let_evlis
  967.     (lambda (h r k tau)
  968.         (_eval (cadr (car h))
  969.                r
  970.                (lambda (v tau)
  971.                    (if (null? (cdr h))
  972.                            (k (list v) tau)
  973.                            (_let_evlis (cdr h)
  974.                                        r
  975.                                        (lambda (lv tau)
  976.                                            (k (cons v lv) tau))
  977.                                        tau)))
  978.                tau)))
  979.  
  980. (define _let_idlis
  981.     (lambda (h)         ; (map car h)
  982.         (if (null? h)
  983.             '()
  984.             (cons (caar h) (_let_idlis (cdr h))))))
  985.  
  986.  
  987. (define _letrec                 ; assumes a well-formed letrec construction
  988.     (lambda (l r k tau)
  989.         (if (null? (car l))
  990.             (_eval (cadr l) r k tau)
  991.             (let ((r (_extend_env (_let_idlis (car l)) '() r)))
  992.                 (_let_evlis (car l)
  993.                             r
  994.                             (lambda (lv tau)
  995.                                 (begin
  996.                                     (set-cdr! (car r) lv)
  997.                                     (_eval (cadr l) r k tau)))
  998.                             tau)))))
  999.  
  1000.  
  1001. (define _rec                    ; assumes a well-formed rec construction
  1002.     (lambda (l r k tau)
  1003.         (let ((r (_extend_env (list (car l)) '() r)))
  1004.             (_eval (cadr l) r (lambda (a tau)
  1005.                                   (begin
  1006.                                       (set-cdr! (car r) (list a))
  1007.                                       (k a tau))) tau))))
  1008.  
  1009.  
  1010. (define _let*                   ; assumes a well-formed let* construction
  1011.     (lambda (l r k tau)
  1012.         (_let*_evlis (car l) (cadr l) r k tau)))
  1013.  
  1014. (define _let*_evlis
  1015.     (lambda (h b r k tau)
  1016.         (if (null? h)
  1017.             (_eval b r k tau)
  1018.             (_eval (cadr (car h))
  1019.                    r
  1020.                    (lambda (a tau)
  1021.                        (_let*_evlis (cdr h)
  1022.                                     b
  1023.                                     (_extend_env (list (caar h)) (list a) r)
  1024.                                     k
  1025.                                     tau))
  1026.                    tau))))
  1027.  
  1028.  
  1029.  
  1030. ; Blond provides the usual conditional cond:
  1031. (define _cond
  1032.     (lambda (l r k tau)
  1033.         (if (null? l)
  1034.             (k "unmatched-cond" tau)
  1035.             (if (equal? (caar l) 'else)
  1036.                 (_eval (cadr (car l)) r k tau)
  1037.                 (_eval (caar l)
  1038.                        r
  1039.                        (lambda (a tau)
  1040.                            (if (or (equal? a #f) (null? a))
  1041.                                (_cond (cdr l) r k tau)
  1042.                                (_eval (cadr (car l)) r k tau)))
  1043.                        tau)))))
  1044.  
  1045.  
  1046.  
  1047. ; Both a reified instance of the initial environment and a reified
  1048. ; instance of a bottom level loop continuation are available:
  1049. (define _reify-new-environment
  1050.     (lambda ()
  1051.         (_env-up (make-initial-environment))))
  1052.  
  1053.  
  1054. (define _reify-new-continuation
  1055.     (lambda (l r k tau)
  1056.         (case (length l)
  1057.             (1
  1058.                 (_eval (car l)
  1059.                        r
  1060.                        (lambda (level tau)
  1061.                            (k (_cont-up (_generate_toplevel-continuation
  1062.                                             level
  1063.                                             (make-initial-environment))) tau))
  1064.                        tau))
  1065.             (2
  1066.                 (_eval (car l)
  1067.                        r
  1068.                        (lambda (level tau)
  1069.                            (_eval (cadr l)
  1070.                                   r
  1071.                                   (lambda (env tau)
  1072.                                       (if (_environment? env)
  1073.                                           (k (_cont-up
  1074.                                              (_generate_toplevel-continuation
  1075.                                                  level (_env-down env)))
  1076.                                              tau)
  1077.                                           (_wrong '_reify-new-continuation
  1078.                                                   "not a reified environment"
  1079.                                                   env)))
  1080.                                   tau))
  1081.                        tau))
  1082.             (else
  1083.                 (_wrong '_reify-new-continuation "arity mismatch" l)))))
  1084.  
  1085.  
  1086. ; Continuations can be applied in a pushy or in a jumpy mode:
  1087. (define _continuation-mode
  1088.     (lambda ()
  1089.         (if (eq? _apply_continuation _apply_continuation-jumpy)
  1090.             'jumpy
  1091.             'pushy)))
  1092.  
  1093.  
  1094. (define _switch-continuation-mode
  1095.     (lambda ()
  1096.         (if (eq? _apply_continuation _apply_continuation-jumpy)
  1097.             (begin
  1098.                 (set! _apply_continuation _apply_continuation-pushy)
  1099.                 'pushy)
  1100.             (begin
  1101.                 (set! _apply_continuation _apply_continuation-jumpy)
  1102.                 'jumpy))))
  1103.  
  1104.  
  1105. ; Ending a session ignores the current continuation and meta-continuation:
  1106. (define _blond-exit
  1107.     (lambda (l r k tau)
  1108.         "farvel!"))
  1109.  
  1110.  
  1111.  
  1112. ; ----- the initial environment -----------------------------------------------
  1113.  
  1114. (define table-common-identifiers
  1115.       '(nil
  1116.         car cdr
  1117.         caar cadr
  1118.         cdar cddr
  1119.         caddr cdddr
  1120.         last-pair
  1121.         null? atom? pair?
  1122.         number? string? symbol?
  1123.         zero? add1 sub1
  1124.         + - *
  1125.         cons equal?
  1126.         = boolean?
  1127.         negative? positive?
  1128.         procedure?
  1129.         quote
  1130.         lambda
  1131.         delta meaning gamma
  1132.         if ef
  1133.         common-define define
  1134.         set!
  1135.         case
  1136.         and or
  1137.         list
  1138.         set-car! set-cdr!
  1139.         begin
  1140.         display print
  1141.         pretty-print newline
  1142.         not length
  1143.         load mute-load read
  1144.         open-input-file eof-object?
  1145.         close-input-port
  1146.         flush-output
  1147.         openloop
  1148.         extend-reified-environment
  1149.         let letrec
  1150.         rec let*
  1151.         cond
  1152.         blond-exit
  1153.         reify-new-environment
  1154.         reify-new-continuation
  1155.         continuation-mode
  1156.         switch-continuation-mode
  1157.         ))
  1158.  
  1159. (define _inSubr
  1160.     (lambda (arity function-value)
  1161.         (list 'subr arity function-value)))
  1162.  
  1163. (define _inFsubr
  1164.     (lambda (arity function-value)
  1165.         (list 'fsubr arity function-value)))
  1166.  
  1167.  
  1168. (define table-common-values
  1169.   (list ()
  1170.         (_inSubr 1 car) (_inSubr 1 cdr)
  1171.         (_inSubr 1 caar) (_inSubr 1 cadr)
  1172.         (_inSubr 1 cdar) (_inSubr 1 cddr)
  1173.         (_inSubr 1 caddr) (_inSubr 1 cdddr)
  1174.         (_inSubr 1 last-pair)
  1175.         (_inSubr 1 null?) (_inSubr 1 atom?) (_inSubr 1 pair?)
  1176.         (_inSubr 1 number?) (_inSubr 1 string?) (_inSubr 1 symbol?)
  1177.         (_inSubr 1 zero?) (_inSubr 1 add1) (_inSubr 1 sub1)
  1178.         (_inSubr 2 +) (_inSubr 2 -) (_inSubr 2 *)
  1179.         (_inSubr 2 cons) (_inSubr 2 equal?)
  1180.         (_inSubr 2 =) (_inSubr 1 boolean?)
  1181.         (_inSubr 1 negative?) (_inSubr 1 positive?)
  1182.         (_inSubr 1 _applicable?)
  1183.         (_inFsubr 1 _quote)
  1184.         (_inFsubr 2 _lambda)
  1185.         (_inFsubr 2 _delta) (_inFsubr 3 _meaning) (_inFsubr 2 _gamma)
  1186.         (_inFsubr 3 _if) (_inSubr 3 _ef)
  1187.         (_inFsubr 2 _common-define) (_inFsubr 2 _define)
  1188.         (_inFsubr 2 _set!)
  1189.         (_inFsubr 0 _case)
  1190.         (_inFsubr 0 _and) (_inFsubr 0 _or)
  1191.         (_inFsubr 0 _evlis)
  1192.         (_inSubr 2 set-car!) (_inSubr 2 set-cdr!)
  1193.         (_inFsubr 0 _begin)
  1194.         (_inSubr 1 display) (_inSubr 1 pretty-print)
  1195.         (_inSubr 1 pretty-print) (_inSubr 0 newline)
  1196.         (_inSubr 1 not) (_inSubr 1 length)
  1197.         (_inFsubr 1 _load) (_inFsubr 1 _mute-load) (_inFsubr 0 _read)
  1198.         (_inSubr 1 open-input-file) (_inSubr 1 eof-object?)
  1199.         (_inSubr 1 close-input-port)
  1200.         (_inSubr 0 flush-output)
  1201.         (_inFsubr 0 _openloop)
  1202.         (_inFsubr 3 _extend-reified-environment)
  1203.         (_inFsubr 2 _let) (_inFsubr 2 _letrec)
  1204.         (_inFsubr 2 _rec) (_inFsubr 2 _let*)
  1205.         (_inFsubr 0 _cond)
  1206.         (_inFsubr 0 _blond-exit)
  1207.         (_inSubr 0 _reify-new-environment)
  1208.         (_inFsubr 0 _reify-new-continuation)
  1209.         (_inSubr 0 _continuation-mode)
  1210.         (_inSubr 0 _switch-continuation-mode)
  1211.         ))
  1212.  
  1213.  
  1214.  
  1215. ; Miscalleneous:
  1216. (define _wrong
  1217.     list)
  1218.  
  1219. (define _constant?
  1220.     (lambda (x)
  1221.         (or (null? x)
  1222.             (number? x)
  1223.             (string? x)
  1224.             (boolean? x))))
  1225.  
  1226. (define _identifier?
  1227.     symbol?)
  1228.  
  1229. (define _index
  1230.     (lambda (i l)
  1231.         (_index_loop i 0 l)))
  1232.  
  1233. (define _index_loop
  1234.     (lambda (i n l)
  1235.         (if (null? l)
  1236.             -1
  1237.             (if (equal? i (car l))
  1238.                 n
  1239.                 (_index_loop i (add1 n) (cdr l))))))
  1240.  
  1241. (define isNatural?
  1242.     (lambda (n)
  1243.     (>= n 0)))
  1244.  
  1245. (define _nth
  1246.     (lambda (n l)
  1247.         (list-tail l n)))
  1248.  
  1249. (define _fetch-ftype car)
  1250. (define _fetch-arity cadr)
  1251. (define _fetch-value caddr)
  1252.  
  1253. ; Basic lexical environment extension:
  1254. (define _extend_env
  1255.     (lambda (par l env)
  1256.         (cons (cons par l) env)))
  1257.  
  1258.  
  1259.  
  1260. ; ----- how Blond hangs together ----------------------------------------------
  1261.  
  1262. ; The starting point:
  1263. (define blond
  1264.     (lambda ()
  1265.         ((_generate_toplevel-continuation initial-level
  1266.                                           (make-initial-environment))
  1267.              blond-banner (initial-meta-continuation initial-level))))
  1268.  
  1269. ; The initial level and how to manifest a level above it:
  1270. (define initial-level 0)
  1271. (define level-above add1)
  1272.  
  1273. ; The generation of an empty global environment:
  1274. (define make-initial-environment
  1275.     (lambda ()
  1276.         (_extend_env () () ())))
  1277.  
  1278.  
  1279. ; Some fantasy:
  1280. (define blond-banner        ; cf. Full Metal Jacket, Stanley Kubrick (1987)
  1281.     "Is it John McCarthy or is it me?")
  1282. (define blond-banner        ; cf. Brazil, Terry Gyndham (1985)
  1283.     "It's okay, I don't like you either.")
  1284. (define blond-banner        ; "til tjeneste" means "at your service"
  1285.     "til tjeneste")        ; it is an old-fashioned formula in Danish
  1286. (define blond-banner
  1287.     "started up")
  1288. (define blond-banner        ; cf. 3-Lisp
  1289.     "[Thud.]")
  1290. (define blond-banner
  1291.     "toplevel")
  1292. (define blond-banner
  1293.     "blond")
  1294. (define blond-banner
  1295.     "-*-")
  1296. (define blond-banner
  1297.     "Blond is winning again")
  1298. (define blond-banner        ; a la Brown
  1299.     "starting-up")
  1300. (define blond-banner
  1301.     "bottom-level")
  1302.  
  1303.  
  1304. ; A self-generating initial meta-continuation:
  1305. (define initial-meta-continuation
  1306.     (lambda (level)
  1307.         (let ((an-initial-environment (make-initial-environment)))
  1308.             (lambda (selector)
  1309.                 (case selector
  1310.                     (env
  1311.                         an-initial-environment)
  1312.                     (cont
  1313.                         (_generate_toplevel-continuation
  1314.                             (level-above level)
  1315.                             an-initial-environment))
  1316.                     (meta-continuation
  1317.                         (initial-meta-continuation (level-above level)))
  1318.                     (else
  1319.                         (_error foobarbaz)))))))
  1320.  
  1321.  
  1322. ; How to get the top-most environment:
  1323. (define _top-env
  1324.     (lambda (meta-continuation)
  1325.         (meta-continuation 'env)))
  1326.  
  1327. ; How to get the top-most continuation:
  1328. (define _top-cont
  1329.     (lambda (meta-continuation)
  1330.         (meta-continuation 'cont)))
  1331.  
  1332. ; How to get the next meta-continuation:
  1333. (define _meta-pop
  1334.     (lambda (meta-continuation)
  1335.         (meta-continuation 'meta-continuation)))
  1336.  
  1337. ; How to get a new meta-continuation:
  1338. (define _meta-push
  1339.     (lambda (r k tau)
  1340.         (lambda (selector)
  1341.             (case selector
  1342.                 (env r)
  1343.                 (cont k)
  1344.                 (meta-continuation tau)
  1345.                 (else (_error foobarbaz))))))
  1346.  
  1347.  
  1348. ; Generation of a new top-level loop:
  1349. (define _generate_toplevel-continuation
  1350.     (lambda (my-level my-environment)
  1351.         (letrec ((elementary-loop
  1352.                     (lambda (iteration)
  1353.                         (lambda (val meta-continuation)
  1354.                             (begin
  1355.                                 (_print my-level iteration val)
  1356.                                 (_eval (read)
  1357.                                        my-environment
  1358.                                        (elementary-loop
  1359.                                             (next-iteration iteration))
  1360.                                        meta-continuation))))))
  1361.             (elementary-loop first-iteration))))
  1362.  
  1363. ; The first iteration and how to manifest the following ones:
  1364. (define first-iteration 0)
  1365. (define next-iteration add1)
  1366.  
  1367.  
  1368.  
  1369. ; A display mechanism for the prompts:
  1370. (define _print
  1371.     (lambda (l i v)
  1372.         (begin
  1373.             (display l)
  1374.             (display "-")
  1375.             (display i)
  1376.             (display ": ")
  1377.             (pretty-print v)
  1378. ;           (newline)           ; in the case it was just (display v)
  1379.             (display l)
  1380.             (display "-")
  1381.             (display (next-iteration i))
  1382.             (display "> ")
  1383.             (flush-output))))
  1384.  
  1385. ; ----- end of the file -------------------------------------------------------
  1386.  
  1387.  
  1388.